﻿#include "AddressBook.h"
#include <iostream>
#include <fstream>
#include <map>
//#include<cctype>
#include <algorithm>
//#define cout std::cout
//#define endl std::endl
//using namespace std;

AddressBook::AddressBook() 
{
	loadFromFile();
}

AddressBook::~AddressBook() {
	saveToFile();
}

void AddressBook::muneDisplay() {
	std::cout << "\n===== 通讯录系统 =====" << std::endl;
	std::cout << "1." << "添加联系人" << std::endl;
	std::cout << "2." << "显示联系人" << std::endl;
	std::cout << "3." << "删除联系人" << std::endl;
	std::cout << "4." << "查找联系人" << std::endl;
	std::cout << "5." << "修改联系人" << std::endl;
	std::cout << "6." << "清空联系人" << std::endl;
	std::cout << "0." << "退出" << std::endl;
};


void AddressBook::loadFromFile()
{
	std::ifstream ifsContacts(AddressBook::filename);
	if (!ifsContacts.is_open())
	{
		std::cout << "未找到数据文件，将自动创建文件。" << std::endl;
		std::ofstream createFile(AddressBook::filename);
		if (!createFile)
		{
			std::cerr << "创建文件失败" << std::endl;
		}
		return;
	}

	Contact contact;
	//第一种写法
	//while (getline(file, contact.name)) {
	//	getline(file, contact.phone);
	//	getline(file, contact.address);
	//	string line;
	//	getline(file, line);
	//	contacts.push_back(contact);
	//}

	//第二种写法
	std::string name, phone, address,group;
	while (getline(ifsContacts, name) && getline(ifsContacts, phone) && getline(ifsContacts, address) && getline(ifsContacts,group)) {
		contacts.push_back(Contact{ name,phone,address,group });
		std::string line_contact;
		getline(ifsContacts, line_contact);
	}
	ifsContacts.close();
	std::cout << "成功加载 " << contacts.size() << " 个联系人。" << std::endl;
}

void AddressBook::saveToFile() const
{
	std::ofstream ofsFile(filename);
	if (!ofsFile.is_open())
	{
		std::cerr << "无法打开文件保存数据" << std::endl;
		return;
	}
	for (const auto& contact : contacts) {//语法糖==for(const it = contacts.begin();it != contancts.end();++it)
		ofsFile << contact.name << std::endl;
		ofsFile << contact.phone << std::endl;
		ofsFile << contact.address << std::endl;
		ofsFile << contact.group << std::endl;
		ofsFile << "-------------------" << std::endl;
	}
	ofsFile.close();
}


bool AddressBook::deleteContact(const std::string& name) {
	for (auto contactIterator = contacts.begin();contactIterator != contacts.end();++contactIterator) {
		if (contactIterator->name == name) {
			std::cout << "确定要删除 " << name << " 吗？(y/n): ";
			char confirm;
			std::cin >> confirm;
			std::cin.ignore();
			if (confirm == 'y' || confirm == 'Y') {
				contacts.erase(contactIterator);
				std::cout << "联系人已删除" << std::endl;
				return true;
			}
			else {
				std::cout << "操作已取消" << std::endl;
				
			}
			
		}
		
	}
	return false;
}
void AddressBook::clearAll() {
	
	std::cout << "确定要清空所有联系人吗？(y/n): ";
	char confirm;
	std::cin >> confirm;
	std::cin.ignore();
	if (confirm == 'y' || confirm == 'Y') {
		contacts.clear();
		std::cout << "通讯录已经清空" << std::endl;
	}
	else {
		std::cout << "操作已取消" << std::endl;
	}
}
//添加用户
void AddressBook::addContact(Contact contact) {
	for (const auto& contactEntity:contacts)
	{
		if (contactEntity.name == contact.name) {
			std::cout << "错误：联系人 '" << contact.name << "' 已存在！" << std::endl;
			return;
		}
		contacts.push_back(contact);
		std::cout << "联系人 '" << contact.name << "' 添加成功！" << std::endl;
		saveToFile();
	}
}

void AddressBook::findContact(const std::string& name) {
	//bool found = false;
	for (const auto& contact:contacts)
	{
		if (contact.name==name)
		{
			std::cout << "┌───────────────────────────────────" << std::endl;
			std::cout << "│ 姓名: " << contact.name << std::endl;
			std::cout << "│ 电话: " << contact.phone << std::endl;
			std::cout << "│ 地址: " << contact.address << std::endl;
			std::cout << "│ 分组: " << contact.group << std::endl;
			std::cout << "└───────────────────────────────────" << std::endl;
			//found = true;
			return;
		}

	}
	std::cout << "未找到联系人 '" << name << "'！" << std::endl;
	//if (!found) {
	//	cout<< "未找到联系人 '" << name << "'！" << std::endl;
	//}
}

void updateElement(const std::string& prompt, std::string& element) {
	std::string input;
	std::cout << prompt << std::endl;
	getline(std::cin, input);
	if (!input.empty()) {
		element = input;
	}
}
void AddressBook::modifyContact(const std::string& name) {
	//bool found = false;
	for (auto& contact:contacts)
	{
		if (contact.name == name) {
			
			updateElement("请重新输入姓名", contact.name);
			updateElement("请输入电话号码", contact.phone);
			updateElement("请输入新地址", contact.address);
			updateElement("请输入新分组", contact.group);
			//std::string input;
			//std::string input;
			//std::cout << "请重新输入姓名" << std::endl;
			//getline(std::cin, input);
			//if (!input.empty()) {
			//	contact.name = input;
			//}
			//std::cout << "请输入电话号码" << std::endl;
			//getline(std::cin, input);
			//if (!input.empty())
			//{
			//	contact.phone = input;
			//}
			//std::cout << "请输入新地址" << std::endl;
			//getline(std::cin, input);
			//if (!input.empty())
			//{
			//	contact.address = input;
			//}
			//std::cout << "请输入新分组" << std::endl;
			//getline(std::cin, input);
			//if (!input.empty())
			//{
			//	contact.group = input;
			//}
			std::cout << "联系人修改成功" << std::endl;
			saveToFile();
			//found = true;
			return;
		}
	}
	std::cout << "未找到联系人" << std::endl;
	//if (!found) {
	//	std::cout << "未找到联系人" << std::endl;
	//}
}


bool AddressBook::isValidPhone(const std::string& phone) {
	//int digitCont = 0;
	//for (char c : phone) {
	//	if (isdigit(c))
	//	{
	//		digitCont++;
	//	}
	//}
	//return digitCont >= 10;
	return std::count_if(phone.begin(), phone.end(), ::isdigit) >= 10;
}

void AddressBook::displayConstAll() const{
	if (contacts.empty())
	{
		std::cout << "通讯录为空" << std::endl;
		return;
	}
	std::map<std::string,int> groupStats;
	for (const auto& contact : contacts) {
		groupStats[contact.group]++;
	}
	for (const auto& pair : groupStats) {
		std::cout << "分组[" << pair.first << "];" << pair.second << "人" << std::endl;
	}
	int index = 1;
	for (const auto& contact : contacts)
	{
		std::cout << "[" << index++ << "] 姓名: " << contact.name << std::endl;
		std::cout << "    电话: " << contact.phone << std::endl;
		std::cout << "    地址: " << contact.address << std::endl;
		std::cout << "    分组: " << contact.group << std::endl;
		std::cout << "------------------------" << std::endl;
	}
	std::cout << "总计: " << contacts.size() << " 个联系人" << std::endl;
}


void AddressBook::selectMenu(const int select) {
	std::cout << "请输入选择" << std::endl;
	int choice;
	std::cin >> choice;
	std::cin.ignore();

	switch (choice) {
	case 0:
		std::cout << "感谢使用通讯录系统！" << std::endl;
		break;
	case 1: {
		Contact newContact;
		std::cout << "请输入姓名";
		getline(std::cin, newContact.name);
		while (true)
		{	
			std::cout << "请输入电话";
			std::string phone;
			getline(std::cin, phone);
			if (isValidPhone(phone)) {
				std::cout << "电话号码格式不正确！" << std::endl;
				break;
			}
			newContact.phone = phone;

			std::cout << "请输入地址";
			getline(std::cin, newContact.address);
			if (newContact.address.empty())
			{
				newContact.address = "";
			}
			std::cout << "请输入分组";
			getline(std::cin, newContact.group);
			if (newContact.group.empty()) {
				newContact.group = "未分组";
			}
			contacts.push_back(newContact);
			system("按回车键继续");
		}
		break;
	}
	case 2:
		displayConstAll();
		break;
	case 3: {

		std::cout << "请输入要删除的联系人姓名"<<std::endl;
		std::string name;
		getline(std::cin, name);
		deleteContact(name);
		break;
	}
	case 4: {
		std::string name;
		std::cout << "请输入要查找的联系人姓名"<<std::endl;
		getline(std::cin, name);
		findContact(name);
		break;
	}
	case 5: {
		std::string name;
		std::cout << "请输入要修改的联系人姓名" << std::endl;
		getline(std::cin, name);
		modifyContact(name);
		break;
	}
	case 6:
		clearAll();
		break;
	default:
		std::cout << "输入无效" << std::endl;
	}
}